package com.probox.modules.controller.wechat;

import com.alibaba.fastjson.JSONObject;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.entity.domain.wechat.broadcast.Good;
import com.probox.common.entity.domain.wechat.broadcast.GoodsInfo;
import com.probox.common.entity.domain.wechat.vo.QueryGood;
import com.probox.common.entity.service.wechat.WechatBroadcastGoodsService;
import com.probox.common.entity.service.wechat.WechatMiniService;
import com.probox.common.entity.utils.ImgFileUtil;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;

/**
 * @author Kite
 * @date 2021/7/9
 */
@RestController
@RequestMapping("/wechat/broadcast/goods")
public class WechatBroadcastGoodsController extends BaseController {

    @Autowired
    private WechatBroadcastGoodsService wechatBroadcastGoodsService;

    @Autowired
    private WechatMiniService wechatMiniService;


    /**
     * 商品添加并提审
     */
    @ApiOperation(value = "商品添加并提审")
    @Log(title = "商品添加并提审", businessType = BusinessType.GRANT)
    @PostMapping("/addGood")
    public AjaxResult addGood(@RequestBody GoodsInfo goodsInfo) {
        return AjaxResult.success(wechatBroadcastGoodsService.addGood(goodsInfo));
    }

    /**
     * 撤回商品审核
     */
    @ApiOperation(value = "撤回商品审核")
    @Log(title = "撤回商品审核", businessType = BusinessType.GRANT)
    @PostMapping("/resetauditAudit")
    public AjaxResult resetauditAudit(@RequestBody QueryGood queryGood) {
        return AjaxResult.success(wechatBroadcastGoodsService.resetauditAudit(queryGood));
    }

    /**
     * 重新提交商品审核
     */
    @ApiOperation(value = "重新提交商品审核")
    @Log(title = "重新提交商品审核", businessType = BusinessType.GRANT)
    @PostMapping("/submitAudit/{goodsId}")
    public AjaxResult submitAudit(@PathVariable Integer goodsId) {
        return AjaxResult.success(wechatBroadcastGoodsService.submitAudit(goodsId));
    }


    /**
     * 删除商品
     */
    @ApiOperation(value = "删除商品")
    @Log(title = "删除商品", businessType = BusinessType.GRANT)
    @PostMapping("/deleteGood/{goodsId}")
    public AjaxResult deleteGood(@PathVariable Integer goodsId) {
        return AjaxResult.success(wechatBroadcastGoodsService.deleteGood(goodsId));
    }

    /**
     * 更新商品
     */
    @ApiOperation(value = "更新商品")
    @Log(title = "更新商品", businessType = BusinessType.GRANT)
    @PostMapping("/updateGood")
    public AjaxResult updateGood(@RequestBody Good good) {
        return AjaxResult.success(wechatBroadcastGoodsService.updateGood(good));
    }

    /**
     * 获取商品状态
     */
    @ApiOperation(value = "获取商品状态")
    @Log(title = "获取商品状态", businessType = BusinessType.GRANT)
    @PostMapping("/getGoodState/{goodsIds}")
    public AjaxResult getGoodState(@PathVariable("goodsIds") String[] goodsIds) {
        return AjaxResult.success(wechatBroadcastGoodsService.getGoodState(goodsIds));
    }
    /**
     * 获取商品列表
     */
    @ApiOperation(value = "获取商品列表")
    @Log(title = "获取商品列表", businessType = BusinessType.GRANT)
    @GetMapping("/getGoodsList")
    public AjaxResult getGoodsList(QueryGood queryRoomVo) {
        return AjaxResult.success(wechatBroadcastGoodsService.getGoodsList(queryRoomVo));
    }


    /**
     * 根据图片地址上传至微信临时素材
     */
    @ApiOperation(value = "根据图片地址上传至微信临时素材")
    @Log(title = "根据图片地址上传至微信临时素材", businessType = BusinessType.GRANT)
    @GetMapping("/updateWaChatImg")
    public AjaxResult updateWaChatImg(String url) throws Exception {
        url = url+"-300px";
        logger.info("新增临时素材上次原图：{}",url);
        File file = ImgFileUtil.downloadWxImage(url);
        wechatMiniService.refreshToken("applet");
        JSONObject rst = new JSONObject();
        try {
            rst = wechatMiniService.uploadMedia("image", SecurityUtils.getPlatformId().toString(),"applet", file);
        }catch (Exception e){
            throw new BaseException("添加测试商品请重新上传图片");
        }

        return AjaxResult.success(rst);

    }


}
